package com.china08.yxyapi.service.sws.teaching.impl;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.aggregation.TypedAggregation;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import com.china08.yxyapi.entity.mg.esbag.YxyEsbagTeachingTrack;
import com.china08.yxyapi.entity.mg.lecture.YxyLecturePack;
import com.china08.yxyapi.entity.mg.lecture.YxyLecturePack.Res;
import com.china08.yxyapi.entity.mg.lecture.YxyLecturePrePack;
import com.china08.yxyapi.repository.mg.lecture.YxyLecturePackRepository;
import com.china08.yxyapi.service.sws.teaching.LecturePackStatisticsService;
import com.china08.yxyapi.util.DateUtils;
import com.google.gson.Gson;

@Service
public class LecturePackStatisticsServiceImpl implements LecturePackStatisticsService {

    private static final Logger LOGGER = Logger.getLogger(LecturePackStatisticsServiceImpl.class);

    @Value(value = "${yx.rest.api.yx.path}")
    private String yxyApiYxUrl;

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private YxyLecturePackRepository yxyLecturePackRepository;

    @Autowired
    private RestTemplate restTemplate;

    // pack——Class
    @Override
    public void getPackForClass(Integer day) throws ParseException {
        Date dayDate = DateUtils.getBeforeDay(DateUtils.formatStrDate4(day));
        Date startDate = DateUtils.getFirstDay(dayDate);
        Date endDate = DateUtils.getTimesnight(dayDate);

        Criteria criteria = Criteria.where("useDate").gte(DateUtils.formatDate4(startDate))
                .lte(DateUtils.formatDate4(endDate));
        criteria.and("isUse").is(true);

        // 获取考试信息
        TypedAggregation<YxyLecturePack> aggregation = Aggregation.newAggregation(YxyLecturePack.class,
                Aggregation.match(criteria),
                Aggregation.group("classId")//
                        .sum("usedCount").as("usedCount")//
                        .sum("teachingCount").as("teachingCount")//
                        .first("classId").as("classId")//
                        .first("className").as("className")//
                        .first("schoolNick").as("schoolNick")//
                        .first("schoolId").as("schoolId"));

        AggregationResults<ClassPackModel> result = mongoTemplate.aggregate(aggregation, ClassPackModel.class);
        List<ClassPackModel> classPackModelLst = result.getMappedResults();
        List<ClassPackModel> sendDataLst = new ArrayList<ClassPackModel>();

        Integer index = 0;
        for (ClassPackModel classPackModel : classPackModelLst) {

            sendDataLst.add(classPackModel);
            index++;
            if (index == 100) {
                try {
                    HttpHeaders headers = new HttpHeaders();
                    MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
                    headers.setContentType(type);
                    headers.add("Accept", MediaType.APPLICATION_JSON_UTF8_VALUE.toString());
                    Gson gson = new Gson();
                    StringBuffer objectBuffer = new StringBuffer();
                    objectBuffer.append("{");
                    objectBuffer.append("\"classPackModelLst\":" + gson.toJson(sendDataLst) + ",");
                    objectBuffer.append("\"dateTime\":" + DateUtils.formatDate4(dayDate));
                    objectBuffer.append("}");
                    HttpEntity<String> formEntity = new HttpEntity<String>(objectBuffer.toString(), headers);
                    String httpResult = restTemplate.postForObject(
                            yxyApiYxUrl + "/comm/school/sws/teaching/teachingclass/pack", formEntity, String.class);
                    if (httpResult.contains("success")) {
                        System.out.println("getPackForClass方法执行ok");
                    }
                } catch (Exception e) {
                    LOGGER.info("getPackForClass方法执行错误：" + e.getMessage());
                }
                sendDataLst = new ArrayList<ClassPackModel>();
                index = 0;
            }
        }
        if (sendDataLst.size() > 0) {
            try {
                HttpHeaders headers = new HttpHeaders();
                MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
                headers.setContentType(type);
                headers.add("Accept", MediaType.APPLICATION_JSON_UTF8_VALUE.toString());
                Gson gson = new Gson();
                StringBuffer objectBuffer = new StringBuffer();
                objectBuffer.append("{");
                objectBuffer.append("\"classPackModelLst\":" + gson.toJson(sendDataLst) + ",");
                objectBuffer.append("\"dateTime\":" + DateUtils.formatDate4(dayDate));
                objectBuffer.append("}");
                HttpEntity<String> formEntity = new HttpEntity<String>(objectBuffer.toString(), headers);
                String httpResult = restTemplate.postForObject(
                        yxyApiYxUrl + "/comm/school/sws/teaching/teachingclass/pack", formEntity, String.class);
                if (httpResult.contains("success")) {
                    System.out.println("getPackForClass方法执行ok");
                }
            } catch (Exception e) {
                LOGGER.info("getPackForClass方法执行错误：" + e.getMessage());
            }
        }
    }

    protected static class ClassPackModel {
        public Long usedCount;
        public Long teachingCount;

        public String classId;
        public String className;
        public String schoolId;
        public String schoolNick;
    }

    // pack——School
    @Override
    public void getPackForSchool(Integer day) throws ParseException {
        Date dayDate = DateUtils.getBeforeDay(DateUtils.formatStrDate4(day));
        Date startDate = DateUtils.getFirstDay(dayDate);
        Date endDate = DateUtils.getTimesnight(dayDate);

        Criteria criteria = Criteria.where("useDate").gte(DateUtils.formatDate4(startDate))
                .lte(DateUtils.formatDate4(endDate));
        criteria.and("isUse").is(true);

        // 获取考试信息
        TypedAggregation<YxyLecturePack> aggregation = Aggregation.newAggregation(YxyLecturePack.class,
                Aggregation.match(criteria),
                Aggregation.group("schoolId")//
                        .sum("usedCount").as("usedCount")//
                        .sum("teachingCount").as("teachingCount")//
                        .first("schoolId").as("schoolId")//
                        .first("schoolNick").as("schoolNick"));

        AggregationResults<SchoolPackModel> result = mongoTemplate.aggregate(aggregation, SchoolPackModel.class);
        List<SchoolPackModel> schoolPackModelLst = result.getMappedResults();
        List<SchoolPackModel> sendDataLst = new ArrayList<SchoolPackModel>();

        Integer index = 0;
        for (SchoolPackModel schoolPackModel : schoolPackModelLst) {

            sendDataLst.add(schoolPackModel);
            index++;
            if (index == 100) {
                try {
                    HttpHeaders headers = new HttpHeaders();
                    MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
                    headers.setContentType(type);
                    headers.add("Accept", MediaType.APPLICATION_JSON_UTF8_VALUE.toString());
                    Gson gson = new Gson();
                    StringBuffer objectBuffer = new StringBuffer();
                    objectBuffer.append("{");
                    objectBuffer.append("\"schoolPackModelLst\":" + gson.toJson(sendDataLst) + ",");
                    objectBuffer.append("\"dateTime\":" + DateUtils.formatDate4(dayDate));
                    objectBuffer.append("}");
                    HttpEntity<String> formEntity = new HttpEntity<String>(objectBuffer.toString(), headers);
                    String httpResult = restTemplate.postForObject(
                            yxyApiYxUrl + "/comm/school/sws/teaching/bureau/pack", formEntity, String.class);
                    if (httpResult.contains("success")) {
                        System.out.println("getPackForSchool方法执行ok");
                    }
                } catch (Exception e) {
                    LOGGER.info("getPackForSchool方法执行错误：" + e.getMessage());
                }
                sendDataLst = new ArrayList<SchoolPackModel>();
                index = 0;
            }
        }
        if (sendDataLst.size() > 0) {
            try {
                HttpHeaders headers = new HttpHeaders();
                MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
                headers.setContentType(type);
                headers.add("Accept", MediaType.APPLICATION_JSON_UTF8_VALUE.toString());
                Gson gson = new Gson();
                StringBuffer objectBuffer = new StringBuffer();
                objectBuffer.append("{");
                objectBuffer.append("\"schoolPackModelLst\":" + gson.toJson(sendDataLst) + ",");
                objectBuffer.append("\"dateTime\":" + DateUtils.formatDate4(dayDate));
                objectBuffer.append("}");
                HttpEntity<String> formEntity = new HttpEntity<String>(objectBuffer.toString(), headers);
                String httpResult = restTemplate.postForObject(yxyApiYxUrl + "/comm/school/sws/teaching/bureau/pack",
                        formEntity, String.class);
                if (httpResult.contains("success")) {
                    System.out.println("getPackForSchool方法执行ok");
                }
            } catch (Exception e) {
                LOGGER.info("getPackForSchool方法执行错误：" + e.getMessage());
            }
        }
    }

    protected static class SchoolPackModel {
        public Long usedCount;
        public Long teachingCount;

        public String schoolId;
        public String schoolNick;
    }

    // pack——SubjectAndStage
    @Override
    public void getPackForSubjectAndStage(Integer day) throws ParseException {
        Date dayDate = DateUtils.getBeforeDay(DateUtils.formatStrDate4(day));
        Date startDate = DateUtils.getFirstDay(dayDate);
        Date endDate = DateUtils.getTimesnight(dayDate);

        Criteria criteria = Criteria.where("useDate").gte(DateUtils.formatDate4(startDate))
                .lte(DateUtils.formatDate4(endDate));
        criteria.and("isUse").is(true);
        criteria.and("phaseId").ne(null);
        criteria.and("subjectId").ne(null);

        // 获取考试信息
        TypedAggregation<YxyLecturePack> aggregation = Aggregation.newAggregation(YxyLecturePack.class,
                Aggregation.match(criteria),
                Aggregation.group("phaseId", "subjectId", "schoolId")//
                        .sum("usedCount").as("usedCount")//
                        .sum("teachingCount").as("teachingCount")//
                        .first("schoolId").as("schoolId")//
                        .first("schoolNick").as("schoolNick")//
                        .first("phaseId").as("phaseId")//
                        .first("phaseName").as("phaseName")//
                        .first("subjectId").as("subjectId")//
                        .first("subjectName").as("subjectName"));

        AggregationResults<SubjectAndStagePackModel> result = mongoTemplate.aggregate(aggregation,
                SubjectAndStagePackModel.class);
        List<SubjectAndStagePackModel> subjectAndStagePackModelLst = result.getMappedResults();
        List<SubjectAndStagePackModel> sendDataLst = new ArrayList<SubjectAndStagePackModel>();

        Integer index = 0;
        for (SubjectAndStagePackModel subjectAndStagePackModel : subjectAndStagePackModelLst) {

            if (!StringUtils.isNoneBlank(subjectAndStagePackModel.phaseName)) {
                continue;
            }
            if (!StringUtils.isNoneBlank(subjectAndStagePackModel.subjectName)) {
                continue;
            }

            sendDataLst.add(subjectAndStagePackModel);
            index++;
            if (index == 100) {
                try {
                    HttpHeaders headers = new HttpHeaders();
                    MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
                    headers.setContentType(type);
                    headers.add("Accept", MediaType.APPLICATION_JSON_UTF8_VALUE.toString());
                    Gson gson = new Gson();
                    StringBuffer objectBuffer = new StringBuffer();
                    objectBuffer.append("{");
                    objectBuffer.append("\"subjectAndStagePackModelLst\":" + gson.toJson(sendDataLst) + ",");
                    objectBuffer.append("\"dateTime\":" + DateUtils.formatDate4(dayDate));
                    objectBuffer.append("}");
                    HttpEntity<String> formEntity = new HttpEntity<String>(objectBuffer.toString(), headers);
                    String httpResult = restTemplate.postForObject(
                            yxyApiYxUrl + "/comm/school/sws/teaching/schoolstagesubject/pack", formEntity,
                            String.class);
                    if (httpResult.contains("success")) {
                        System.out.println("getPackForSubjectAndStage方法执行ok");
                    }
                } catch (Exception e) {
                    LOGGER.info("getPackForSubjectAndStage方法执行错误：" + e.getMessage());
                }
                sendDataLst = new ArrayList<SubjectAndStagePackModel>();
                index = 0;
            }
        }
        if (sendDataLst.size() > 0) {
            try {
                HttpHeaders headers = new HttpHeaders();
                MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
                headers.setContentType(type);
                headers.add("Accept", MediaType.APPLICATION_JSON_UTF8_VALUE.toString());
                Gson gson = new Gson();
                StringBuffer objectBuffer = new StringBuffer();
                objectBuffer.append("{");
                objectBuffer.append("\"subjectAndStagePackModelLst\":" + gson.toJson(sendDataLst) + ",");
                objectBuffer.append("\"dateTime\":" + DateUtils.formatDate4(dayDate));
                objectBuffer.append("}");
                HttpEntity<String> formEntity = new HttpEntity<String>(objectBuffer.toString(), headers);
                String httpResult = restTemplate.postForObject(
                        yxyApiYxUrl + "/comm/school/sws/teaching/schoolstagesubject/pack", formEntity, String.class);
                if (httpResult.contains("success")) {
                    System.out.println("getPackForSubjectAndStage方法执行ok");
                }
            } catch (Exception e) {
                LOGGER.info("getPackForSubjectAndStage方法执行错误：" + e.getMessage());
            }
        }
    }

    protected static class SubjectAndStagePackModel {
        public Long usedCount;
        public Long teachingCount;

        public String schoolId;
        public String schoolNick;
        public String phaseId;
        public String phaseName;
        public String subjectId;
        public String subjectName;
    }

    // pack——User
    @Override
    public void getPackForUser(Integer day) throws ParseException {
        Date dayDate = DateUtils.getBeforeDay(DateUtils.formatStrDate4(day));
        Date startDate = DateUtils.getFirstDay(dayDate);
        Date endDate = DateUtils.getTimesnight(dayDate);

        Criteria criteria = Criteria.where("useDate").gte(DateUtils.formatDate4(startDate))
                .lte(DateUtils.formatDate4(endDate));
        criteria.and("isUse").is(true);

        // 获取考试信息
        TypedAggregation<YxyLecturePack> aggregation = Aggregation.newAggregation(YxyLecturePack.class,
                Aggregation.match(criteria),
                Aggregation.group("userId", "schoolId")//
                        .sum("usedCount").as("usedCount")//
                        .sum("teachingCount").as("teachingCount")//
                        .first("schoolId").as("schoolId")//
                        .first("schoolNick").as("schoolNick")//
                        .first("userId").as("userId"));

        AggregationResults<UserPackModel> result = mongoTemplate.aggregate(aggregation, UserPackModel.class);
        List<UserPackModel> userPackModellLst = result.getMappedResults();
        List<UserPackModel> sendDataLst = new ArrayList<UserPackModel>();

        Integer index = 0;
        for (UserPackModel userPackModel : userPackModellLst) {

            sendDataLst.add(userPackModel);
            index++;
            if (index == 100) {
                try {
                    HttpHeaders headers = new HttpHeaders();
                    MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
                    headers.setContentType(type);
                    headers.add("Accept", MediaType.APPLICATION_JSON_UTF8_VALUE.toString());
                    Gson gson = new Gson();
                    StringBuffer objectBuffer = new StringBuffer();
                    objectBuffer.append("{");
                    objectBuffer.append("\"userPackModelLst\":" + gson.toJson(sendDataLst) + ",");
                    objectBuffer.append("\"dateTime\":" + DateUtils.formatDate4(dayDate));
                    objectBuffer.append("}");
                    HttpEntity<String> formEntity = new HttpEntity<String>(objectBuffer.toString(), headers);
                    String httpResult = restTemplate.postForObject(
                            yxyApiYxUrl + "/comm/school/sws/teaching/teacher/pack", formEntity, String.class);
                    if (httpResult.contains("success")) {
                        System.out.println("getPackForUser方法执行ok");
                    }
                } catch (Exception e) {
                    LOGGER.info("getPackForUser方法执行错误：" + e.getMessage());
                }
                sendDataLst = new ArrayList<UserPackModel>();
                index = 0;
            }
        }
        if (sendDataLst.size() > 0) {
            try {
                HttpHeaders headers = new HttpHeaders();
                MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
                headers.setContentType(type);
                headers.add("Accept", MediaType.APPLICATION_JSON_UTF8_VALUE.toString());
                Gson gson = new Gson();
                StringBuffer objectBuffer = new StringBuffer();
                objectBuffer.append("{");
                objectBuffer.append("\"userPackModelLst\":" + gson.toJson(sendDataLst) + ",");
                objectBuffer.append("\"dateTime\":" + DateUtils.formatDate4(dayDate));
                objectBuffer.append("}");
                HttpEntity<String> formEntity = new HttpEntity<String>(objectBuffer.toString(), headers);
                String httpResult = restTemplate.postForObject(yxyApiYxUrl + "/comm/school/sws/teaching/teacher/pack",
                        formEntity, String.class);
                if (httpResult.contains("success")) {
                    System.out.println("getPackForUser方法执行ok");
                }
            } catch (Exception e) {
                LOGGER.info("getPackForUser方法执行错误：" + e.getMessage());
            }
        }
    }

    protected static class UserPackModel {
        public Long usedCount;
        public Long teachingCount;

        public String schoolId;
        public String schoolNick;
        public String userId;
    }

    @Override
    public void getLectureUseType(Integer day) throws ParseException {
        Date dayDate = DateUtils.getBeforeDay(DateUtils.formatStrDate4(day));
        Date startDate = DateUtils.getFirstDay(dayDate);
        Date endDate = DateUtils.getTimesnight(dayDate);

        List<YxyLecturePack> dataLst = yxyLecturePackRepository.findByCreatedDateBetween(startDate, endDate);

        if (dataLst != null && dataLst.size() > 0) {
            Map<String, List<YxyLecturePack>> packSchLst = dataLst.stream()
                    .collect(Collectors.groupingBy(YxyLecturePack::getSchoolId));

            List<UseTypePackModel> packModels = new ArrayList<UseTypePackModel>();
            for (Entry<String, List<YxyLecturePack>> sch : packSchLst.entrySet()) {
                int teaching = 0;
                int proview = 0;
                int homework = 0;
                int test = 0;
                String schName = "";
                UseTypePackModel model = new UseTypePackModel();
                for (YxyLecturePack yxyLecturePack : sch.getValue()) {
                    for (Res res : yxyLecturePack.getResources()) {
                        if (res.getUsesTeaching() == 1) {
                            teaching++;
                        }
                        if (res.getUsesProview() == 1) {
                            proview++;
                        }
                        if (res.getUsesHomework() == 1) {
                            homework++;
                        }
                        if (res.getUsesTest() == 1) {
                            test++;
                        }
                    }
                    schName = yxyLecturePack.getSchoolNick();
                }
                model.teaching = teaching == 0 ? null : new Long(teaching);
                model.proview = proview == 0 ? null : new Long(proview);
                model.homework = homework == 0 ? null : new Long(homework);
                model.test = test == 0 ? null : new Long(test);
                model.schoolId = sch.getKey();
                model.schoolNick = schName;

                Criteria criteria = Criteria.where("endDate").gte(startDate).lte(endDate);
                criteria.and("schoolId").is(model.schoolId);
                Query query = new Query();
                query.addCriteria(criteria);
                Long count = mongoTemplate.count(query, YxyEsbagTeachingTrack.class);
                model.teaching += count;

                packModels.add(model);

                if (packModels.size() == 100) {
                    try {
                        HttpHeaders headers = new HttpHeaders();
                        MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
                        headers.setContentType(type);
                        headers.add("Accept", MediaType.APPLICATION_JSON_UTF8_VALUE.toString());
                        Gson gson = new Gson();
                        StringBuffer objectBuffer = new StringBuffer();
                        objectBuffer.append("{");
                        objectBuffer.append("\"useTypePackModelLst\":" + gson.toJson(packModels) + ",");
                        objectBuffer.append("\"dateTime\":" + DateUtils.formatDate4(dayDate));
                        objectBuffer.append("}");
                        HttpEntity<String> formEntity = new HttpEntity<String>(objectBuffer.toString(), headers);
                        String httpResult = restTemplate.postForObject(
                                yxyApiYxUrl + "/comm/school/sws/teaching/usetype/day", formEntity, String.class);
                        if (httpResult.contains("success")) {
                            System.out.println("getLectureUseType方法执行ok");
                        }
                    } catch (Exception e) {
                        LOGGER.info("getLectureUseType方法执行错误：" + e.getMessage());
                    }
                    packModels = new ArrayList<UseTypePackModel>();
                }
            }
            if (packModels.size() > 0) {
                try {
                    HttpHeaders headers = new HttpHeaders();
                    MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
                    headers.setContentType(type);
                    headers.add("Accept", MediaType.APPLICATION_JSON_UTF8_VALUE.toString());
                    Gson gson = new Gson();
                    StringBuffer objectBuffer = new StringBuffer();
                    objectBuffer.append("{");
                    objectBuffer.append("\"useTypePackModelLst\":" + gson.toJson(packModels) + ",");
                    objectBuffer.append("\"dateTime\":" + DateUtils.formatDate4(dayDate));
                    objectBuffer.append("}");
                    HttpEntity<String> formEntity = new HttpEntity<String>(objectBuffer.toString(), headers);
                    String httpResult = restTemplate.postForObject(
                            yxyApiYxUrl + "/comm/school/sws/teaching/usetype/day", formEntity, String.class);
                    if (httpResult.contains("success")) {
                        System.out.println("getLectureUseType方法执行ok");
                    }
                } catch (Exception e) {
                    LOGGER.info("getLectureUseType方法执行错误：" + e.getMessage());
                }
            }
        }
    }

    protected static class UseTypePackModel {
        public Long teaching;
        public Long proview;
        public Long homework;
        public Long test;
        public String schoolId;
        public String schoolNick;
    }

    // perPack——User
    @Override
    public void getPerPackForUser(Integer day) throws ParseException {
        Date dayDate = DateUtils.getBeforeDay(DateUtils.formatStrDate4(day));
        Date startDate = DateUtils.getFirstDay(dayDate);
        Date endDate = DateUtils.getTimesnight(dayDate);

        Criteria criteria = Criteria.where("createdDate").gte(startDate).lte(endDate);

        // 获取考试信息
        TypedAggregation<YxyLecturePrePack> aggregation = Aggregation.newAggregation(YxyLecturePrePack.class,
                Aggregation.match(criteria),
                Aggregation.group("userId", "schoolId")//
                        .count().as("count")//
                        .first("schoolId").as("schoolId")//
                        .first("schoolNick").as("schoolNick")//
                        .first("userId").as("userId"));

        AggregationResults<UserPerPackModel> result = mongoTemplate.aggregate(aggregation, UserPerPackModel.class);
        List<UserPerPackModel> userPerPackModellLst = result.getMappedResults();
        List<UserPerPackModel> sendDataLst = new ArrayList<UserPerPackModel>();

        Integer index = 0;
        for (UserPerPackModel userPackModel : userPerPackModellLst) {

            sendDataLst.add(userPackModel);
            index++;
            if (index == 100) {
                try {
                    HttpHeaders headers = new HttpHeaders();
                    MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
                    headers.setContentType(type);
                    headers.add("Accept", MediaType.APPLICATION_JSON_UTF8_VALUE.toString());
                    Gson gson = new Gson();
                    StringBuffer objectBuffer = new StringBuffer();
                    objectBuffer.append("{");
                    objectBuffer.append("\"userPerPackModelLst\":" + gson.toJson(sendDataLst) + ",");
                    objectBuffer.append("\"dateTime\":" + DateUtils.formatDate4(dayDate));
                    objectBuffer.append("}");
                    HttpEntity<String> formEntity = new HttpEntity<String>(objectBuffer.toString(), headers);
                    String httpResult = restTemplate.postForObject(
                            yxyApiYxUrl + "/comm/school/sws/teaching/teacher/perpack", formEntity, String.class);
                    if (httpResult.contains("success")) {
                        System.out.println("getPerPackForUser方法执行ok");
                    }
                } catch (Exception e) {
                    LOGGER.info("getPerPackForUser方法执行错误：" + e.getMessage());
                }
                sendDataLst = new ArrayList<UserPerPackModel>();
                index = 0;
            }
        }
        if (sendDataLst.size() > 0) {
            try {
                HttpHeaders headers = new HttpHeaders();
                MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
                headers.setContentType(type);
                headers.add("Accept", MediaType.APPLICATION_JSON_UTF8_VALUE.toString());
                Gson gson = new Gson();
                StringBuffer objectBuffer = new StringBuffer();
                objectBuffer.append("{");
                objectBuffer.append("\"userPerPackModelLst\":" + gson.toJson(sendDataLst) + ",");
                objectBuffer.append("\"dateTime\":" + DateUtils.formatDate4(dayDate));
                objectBuffer.append("}");
                HttpEntity<String> formEntity = new HttpEntity<String>(objectBuffer.toString(), headers);
                String httpResult = restTemplate.postForObject(
                        yxyApiYxUrl + "/comm/school/sws/teaching/teacher/perpack", formEntity, String.class);
                if (httpResult.contains("success")) {
                    System.out.println("getPerPackForUser方法执行ok");
                }
            } catch (Exception e) {
                LOGGER.info("getPerPackForUser方法执行错误：" + e.getMessage());
            }
        }
    }

    protected static class UserPerPackModel {
        public Long count;

        public String schoolId;
        public String schoolNick;
        public String userId;
    }

}
